package net.sunbeta.test.util ;

import java.io.UnsupportedEncodingException ;
import java.net.URLDecoder ;

import org.apache.commons.lang.StringUtils ;

/**
* <p>Title:code tool</p>
* <p>Description:  </p>
* <p>Copyright: flashman.com.cn Copyright (c) 2005</p>
* <p>Company: flashman.com.cn </p>
* @author: jeffzhu
* @version 1.0
*/
public class CharTools {

    /**
     * convert code ISO-8859-1 to GB2312
     * @param text
     * @return
     */
    public String ISO2GB(String text) {
        String result = "" ;
        try {
            result = new String(text.getBytes("ISO-8859-1"), "GB2312") ;
        } catch (UnsupportedEncodingException ex) {
            result = ex.toString() ;
        }
        return result ;
    }

    /**
     * convert code GB2312 to ISO-8859-1
     * @param text
     * @return
     */
    public String GB2ISO(String text) {
        String result = "" ;
        try {
            result = new String(text.getBytes("GB2312"), "ISO-8859-1") ;
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace() ;
        }
        return result ;
    }

    /**
     * Utf8URL encode
     * @param s
     * @return
     */
    public String Utf8URLencode(String text) {
        StringBuffer result = new StringBuffer() ;

        for (int i = 0; i < text.length(); i++) {

            char c = text.charAt(i) ;
            if (c >= 0 && c <= 255) {
                result.append(c) ;
            } else {

                byte[] b = new byte[0] ;
                try {
                    b = Character.toString(c).getBytes("UTF-8") ;
                } catch (Exception ex) {
                }

                for (int j = 0; j < b.length; j++) {
                    int k = b[j] ;
                    if (k < 0)
                        k += 256 ;
                    result.append("%" + Integer.toHexString(k).toUpperCase()) ;
                }

            }
        }

        return result.toString() ;
    }

    /**
     * Utf8URL decode
     * @param text
     * @return
     */
    public String Utf8URLdecode(String text) {
        String result = "" ;
        int p = 0 ;

        if (text != null && text.length() > 0) {
            text = text.toLowerCase() ;
            p = text.indexOf("%e") ;
            if (p == -1)
                return text ;

            while (p != -1) {
                result += text.substring(0, p) ;
                text = text.substring(p, text.length()) ;
                if (text == "" || text.length() < 9)
                    return result ;

                result += CodeToWord(text.substring(0, 9)) ;
                text = text.substring(9, text.length()) ;
                p = text.indexOf("%e") ;
            }

        }

        return result + text ;
    }

    /**
     * utf8URL code converter
     * @param text
     * @return
     */
    private String CodeToWord(String text) {
        String result ;

        if (Utf8codeCheck(text)) {
            byte[] code = new byte[3] ;
            code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256) ;
            code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256) ;
            code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256) ;
            try {
                result = new String(code, "UTF-8") ;
            } catch (UnsupportedEncodingException ex) {
                result = null ;
            }
        } else {
            result = text ;
        }

        return result ;
    }

    /**
     * code is or not valid
     * @param text
     * @return
     */
    private boolean Utf8codeCheck(String text) {
        String sign = "" ;
        if (text.startsWith("%e"))
            for (int i = 0, p = 0; p != -1; i++) {
                p = text.indexOf("%", p) ;
                if (p != -1)
                    p++ ;
                sign += p ;
            }
        return sign.equals("147-1") ;
    }

    /**
     * hash Utf8Url code
     * @param text
     * @return
     */
    public boolean isUtf8Url(String text) {
        text = text.toLowerCase() ;
        int p = text.indexOf("%") ;
        if (p != -1 && text.length() - p > 9) {
            text = text.substring(p, p + 9) ;
        }
        return Utf8codeCheck(text) ;
    }

    /**
     * test
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {

        CharTools charTools = new CharTools() ;

        String url ;

        url = "Memcached%d4%ad%c0%ed%ba%cd%ca%b9%d3%c3%cf%ea%bd%e2.pdf" ;
        if (charTools.isUtf8Url(url)) {
            System.out.println(charTools.Utf8URLdecode(url)) ;
        } else {
            System.out.println(URLDecoder.decode(url, "GB2312")) ;
        }

        String dd = "http://mirrors.sohu.com/ebook/perl/" ;
        String[] ee = StringUtils.split(dd, "/") ;
        System.out.println(ee[ee.length - 1]) ;

    }
}
